- Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathQueenCombinationWithBoxRespect.java
46 lines (33 loc) Β· 825 Bytes
/
QueenCombinationWithBoxRespect.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
packagesection12_Backtracking;
publicclassQueenCombinationWithBoxRespect {
publicstaticvoidmain(String[] args) {
boolean[] board = newboolean[4];
intqueens = 2;
intqueenPlacedSoFar = 0;
Stringans = "";
intcurrentBox = 0;
queenCombinationBox(board, currentBox, queens, queenPlacedSoFar, ans);
}
staticvoidqueenCombinationBox(boolean[] board, intcurrBox, intq, intplaced, Stringans) {
if (q == placed) {
System.out.println(ans);
return;
}
if (currBox > board.length - 1)
return;
// placing the queen
board[currBox] = true;
queenCombinationBox(board, currBox + 1, q, placed + 1, ans + "b" + currBox);
// not placing the queen
board[currBox] = false;
queenCombinationBox(board, currBox + 1, q, placed, ans);
}
}
/* output:
b0b1
b0b2
b0b3
b1b2
b1b3
b2b3
*/